<?php

define('FREE', 0);
define('PAID', 1);
define('TEXT', 0);
define('WAPPUSH', 1);

require_once('Nusoap/nusoap.php');

class SmsGateway {

    protected static $_instance = null;
    protected $_config = null;
    protected $_method = 'sms';
    public static $template = array(
        'send_vod'      => 'Quy khach nhan duoc 1 video tu he thong MobiTV va duoc xem mien phi video nay trong 24 gio ke tu khi nhan. Nhan vao link de xem %url%',
        'send_vod_down' => 'Quy khach nhan duoc 1 video tu he thong MobiTV. Nhan vao link de tai %url%',
    );

    public function __construct() {
        $coreConfig = Vega_App::getInstance()->coreConfig;
        $this->_config = $coreConfig['sms'];
    }

    public static function instance() {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    public function alert($receiver, $content) {
        if (!is_array($receiver))
            $receiver = array($receiver);
        if ($receiver) {
            foreach ($receiver AS $msisdn) {
                $this->sendText($msisdn, $content);
            }
        }
    }

    public function sendText($receiver, $content, $smsId=0, $subject="", $charge=FREE, $msgType=TEXT) {
        return $this->send($receiver, $content, $charge, $msgType, $subject, $smsId);
    }

    public function sendWappush($receiver, $content, $smsId=0, $subject="", $charge=FREE, $msgType=WAPPUSH) {
        return $this->send($receiver, $content, $charge, $msgType, $subject, $smsId);
    }

    private function send($receiver, $content, $charge, $msgType, $subject, $smsId) {
        /*         * ****************
         * 	SOAP CLIENT
         * **************** */
        $startTime = microtime(true);
        //$client = new nusoap_client($this->_config['remote_wsdl']);
        $client = new SoapClient($this->_config['remote_wsdl']);

        $params = array(
            'username' => $this->_config['remote_username'],
            'password' => $this->_config['remote_password'],
            'service_number' => $this->_config['service_number'],
            'sender' => $receiver,
            'receiver' => $receiver,
            'content' => $content,
            'charge' => $charge,
            'msg_type' => $msgType,
            'subject' => $subject,
            'sms_id' => $smsId,
            'smsc' => $this->_config['service_number'],
        );

        //$result = $client->call("sendMT", $params, $this->_config['namespace']);
        $result = $client->sendMT($params['username'], $params['password'], $params['service_number'], $params['sender'], $params['receiver'], $params['content'], $params['charge'], $params['msg_type'], $params['subject'], $params['sms_id'], $params['smsc']);

        // Thời gian xử lý
        $elapsedTime = round((microtime(true) - $startTime) * 1000);

        // Log
        require_once 'Vega/Log/Writer.php';
        $log = new Vega_Log_Writer($this->_config['mt_log_file']);
        $line = sprintf("%s|%s|%s|%s|%s|%s|%s|%s|%s", date("Y-m-d H:i:s"), $receiver, $charge, $msgType, $smsId, $subject, $content, $result, $elapsedTime);
        $log->write($line);
    }

    function validPhoneNumber($phone) {
        if ((bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $phone) && ( strlen($phone) == 11 && !substr_compare($phone, '849', 0, 3) ) || ( strlen($phone) == 12 && !substr_compare($phone, '841', 0, 3) ))
            return true;
        else
            return false;
    }

}